package sg.edu.nus.iss.vmcs;

import java.io.IOException;

import sg.edu.nus.iss.vmcs.abstraction.IChangeGiverService;
import sg.edu.nus.iss.vmcs.abstraction.ICoinReceiverService;
import sg.edu.nus.iss.vmcs.abstraction.IDispenseService;
import sg.edu.nus.iss.vmcs.abstraction.IServiceRepository;
import sg.edu.nus.iss.vmcs.abstraction.IStoreFactory;
import sg.edu.nus.iss.vmcs.abstraction.IStoreService;
import sg.edu.nus.iss.vmcs.abstraction.ITransactionService;
import sg.edu.nus.iss.vmcs.abstraction.PresenterNames;
import sg.edu.nus.iss.vmcs.customer.CustomerPanel;
import sg.edu.nus.iss.vmcs.customer.CustomerPanelPresenter;
import sg.edu.nus.iss.vmcs.simulation.SimulationControlPanel;
import sg.edu.nus.iss.vmcs.simulation.SimulationControlPanelPresenter;
import sg.edu.nus.iss.vmcs.store.StoreService;
import sg.edu.nus.iss.vmcs.system.ChangeGiverService;
import sg.edu.nus.iss.vmcs.system.CoinReceiverService;
import sg.edu.nus.iss.vmcs.system.DispenseService;
import sg.edu.nus.iss.vmcs.system.Environment;
import sg.edu.nus.iss.vmcs.system.PropertyLoaderFactory;
import sg.edu.nus.iss.vmcs.system.ServiceRepository;
import sg.edu.nus.iss.vmcs.system.StoreFactory;
import sg.edu.nus.iss.vmcs.system.TransactionService;
import sg.edu.nus.iss.vmcs.util.VMCSException;

/*
 * @author nyi nyi than
 */

public class VMCSApp
{
	private String propertyFile;

	SimulationControlPanelPresenter simulationPresenter;
	CustomerPanelPresenter customerPanelPresenter;
	IServiceRepository serviceRepository;
	IStoreService storeService;

	public VMCSApp(String propertyFile)
	{
		this.propertyFile = propertyFile;
	}

	public void run() throws VMCSException, IOException
	{
		this.initialize();
		simulationPresenter.display();
	}

	private void initialize() throws VMCSException, IOException
	{
		Environment.initialize(propertyFile);

		serviceRepository = ServiceRepository.GetInstance();

		//using property file
		IStoreFactory storeFactory = new StoreFactory(
				new PropertyLoaderFactory());
		
		//using database
//		IStoreFactory storeFactory = new StoreFactory(
//				new DBLoaderFactory());
		
		storeService = new StoreService(storeFactory);

		
		ICoinReceiverService coinReceiverService = new CoinReceiverService();
		IChangeGiverService changeGiverService = new ChangeGiverService();
		IDispenseService dispenseService = new DispenseService();
		ITransactionService transactionService = new TransactionService(coinReceiverService, changeGiverService, dispenseService);
		
		serviceRepository.addService(IStoreService.class, storeService);
		serviceRepository.addService(ITransactionService.class, transactionService);
		
		simulationPresenter = new SimulationControlPanelPresenter(
				PresenterNames.SIMULATION_PRESENTER);
		SimulationControlPanel simulationPanel = new SimulationControlPanel(
				simulationPresenter);
		simulationPresenter.setView(simulationPanel);

		customerPanelPresenter = new CustomerPanelPresenter(
				PresenterNames.CUSTOMER_PRESENTER);
		CustomerPanel customerPanel = new CustomerPanel(customerPanelPresenter);
		customerPanelPresenter.setView(customerPanel);
	}


}
